/**
 * @fileOverview behaviour util
 * @author huangtonger@aliyun.com
 */
const BaseUtil = require('./base');

module.exports = {
  dragingEdgeEndPoint({
    endPointType,
    edgeModel,
    graph,
    delegation,
    startPoint,
    ev,
    source,
    target
  }) {
    const { item } = ev;
    const shapeObj = graph.getShapeObj('edge', edgeModel);
    const points = endPointType === 'source' ? [ ev, startPoint ] : [ startPoint, ev ];
    if (item) {
      if (endPointType === 'source') {
        target = item;
      } else {
        source = item;
      }
    }
    const path = shapeObj.getPathByPoints({ points, source, target });
    // let activeAnchor;
    // if (item && item.isNode && item.getAnchorPoints().length > 0) {
    //   const linkPoints = item.getLinkPoints(ev);
    //   const nearestAnchorIndex = linkPoints[0].index;
    //   const anchors = item.getAllAnchors();
    //   anchors.forEach(anchor => {
    //     if (anchor.getIndex() === nearestAnchorIndex) {
    //       activeAnchor = anchor;
    //       anchor.setHotspotActived(true);
    //     } else {
    //       anchor.setHotspotActived(false);
    //     }
    //   });
    // }
    delegation.attr('path', path);
    graph.draw();
    // return activeAnchor;
  },
  // 平移组
  panGroup(group, dx, dy, graph) {
    const groupModel = group.getModel();
    BaseUtil.traverseTree(group, child => {
      if (child.type === 'node') {
        const model = child.getModel();
        graph.update(child, {
          x: model.x + dx,
          y: model.y + dy
        });
      }
    }, parent => {
      if (parent.type === 'group') {
        return parent.getChildren();
      }
      return [];
    });
    graph.update(group, {
      x: groupModel.x + dx,
      y: groupModel.y + dy
    });
  },
  dropUpdateEdge({ ev, endPointType, model, diagram }) {
    const noEndEdge = diagram.get('noEndEdge');
    const linkAnchor = diagram.get('linkAnchor');
    const linkNode = diagram.get('linkNode');
    const { item, shape, x, y } = ev;
    const graph = diagram.getGraph();
    graph.emit('beforedropedge');
    if (shape) {
      if (linkAnchor && shape.isAnchor && shape.hasHotspot) {
        const anchor = shape;
        const anchorItem = anchor.getItem();
        if (endPointType === 'target') {
          model.target = anchorItem.id;
          model.targetAnchor = anchor.getIndex();
          return true;
        }
        model.source = anchorItem.id;
        model.sourceAnchor = anchor.getIndex();
        return true;
      }
      if (linkNode && item && item.isNode) {
        if (endPointType === 'target') {
          model.target = item.id;
          return true;
        }
        model.source = item.id;
        return true;
      }
    } else {
      if (noEndEdge) {
        if (endPointType === 'target') {
          model.target = {
            x,
            y
          };
          return true;
        }
        model.source = {
          x,
          y
        };
        return true;
      }
    }
    return false;
  }
};
